<?php

	/**
	 * @see Bike_Form_Renderer_ExtJs_Abstract
	 */
	require_once 'Bike/Form/Renderer/ExtJs/Abstract.php';
	
	/**
	 * @see Bike_Form_Renderer_ExtJs
	 */
	require_once 'Bike/Form/Renderer/ExtJs.php';
	
	
	class Bike_Form_Renderer_ExtJs_Form extends Bike_Form_Renderer_ExtJs_Abstract
	{
		const BIKE_JS_EXT_CLASS = 'Ext.form.Panel';

		protected $_buttons;
		protected $_items;
		protected $_applyButtonsTo;

		public function initWithElement()
		{
			$this->getApplyButtonsTo();
			$this->handleItems();
		}

		public function getForm()
		{			return $this->getElement();
		}

		protected function handleItems()
		{			$this->_items = array();
			$this->_buttons = array();
			foreach($this->getForm() as $Element)
            {            	$rendered = $Element->render(null, Bike_Form_Renderer_ExtJs::TYPE);
            	if($Element->getElement() instanceof Zend_Form_Element_Submit)
            	{            		array_push($this->_buttons, $rendered);
            	}
            	else
            	{
            		array_push($this->_items, $rendered);
            	}
            }
		}

		protected function getButtons()
		{
			return $this->_buttons;
		}

		protected function getItems()
		{
			return $this->_items;
		}

		protected function getApplyButtonsTo()
		{			if(is_null($this->_applyButtonsTo))
			{
				$this->_applyButtonsTo = $this->getForm()->getApplyButtonsTo();
				if(!$this->_applyButtonsTo)
				{
					$this->_applyButtonsTo = array('buttons');
				}
				elseif(is_string($this->_applyButtonsTo))
				{					$this->_applyButtonsTo = array($this->_applyButtonsTo);
				}
				$this->getForm()->unsetApplyButtonsTo();
			}
			return (array) $this->_applyButtonsTo;
		}

		protected function getExtJsSettings()
		{
			$Settings = array_merge((array) parent::getExtJsSettings(), (array) $this->getForm()->getSettings(), array(
				'url' => $this->getForm()->getAttrib('action'),
				'method' => $this->getForm()->getAttrib('method'),
				'title' => $this->getForm()->getLegend(),
				'items' => $this->getItems(),
			));
			foreach($this->getApplyButtonsTo() as $dest)
			{				$Settings[$dest] = $this->getButtons();
			}
			return $Settings;
		}
		
		protected function mergeExtJsSettings()
		{
			$arrays = func_get_args();			
		}
	}
